package homework.homework_11.book;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * @author 朱
 * @date 2019042421:53
 */
public class BookDaoJDBC implements BookDao{
    @Override
    public void add(Book book) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");

            String url="jdbc:mysql://localhost:3306/book";
            String user="root";
            String password=null;
            connection= DriverManager.getConnection(url,user,password);

            String sql="INSERT  INTO manger(`name`,`press`,`author`,`price`)\n" +
                    "VALUE(?,?,?,?)";
            preparedStatement=connection.prepareStatement(sql);

            preparedStatement.setString(1,book.getName());
            preparedStatement.setString(2,book.getPress());
            preparedStatement.setString(3,book.getWriter());
            preparedStatement.setDouble(4,book.getPrice());

            preparedStatement.executeUpdate();
            System.out.println("运行成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void alter(String name,Book book) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");

            String url="jdbc:mysql://localhost:3306/book";
            String user="root";
            String password=null;
            connection=DriverManager.getConnection(url,user,password);

            String sql="UPDATE `manger`\n" +
                    "SET NAME=?,press=?,author=?, price=?\n" +
                    "WHERE `name`=?";
            preparedStatement=connection.prepareStatement(sql);

            preparedStatement.setString(1,book.getName());
            preparedStatement.setString(2,book.getPress());
            preparedStatement.setString(3,book.getWriter());
            preparedStatement.setDouble(4,book.getPrice());
            preparedStatement.setString(5,name);
            preparedStatement.executeUpdate();
            System.out.println("运行成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(String name) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");

            String url="jdbc:mysql://localhost:3306/book";
            String user="root";
            String password=null;
            connection=DriverManager.getConnection(url,user,password);

            String sql="DELETE FROM `manger`\n" +
                    "WHERE NAME=?";
            preparedStatement=connection.prepareStatement(sql);

            preparedStatement.setString(1,name);
            preparedStatement.executeUpdate();
            System.out.println("运行成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
